<?php
/**
 * 名人结构
 * 
 * @author 	jiaowei<jiaowei@178.com>
 * @version $Id: star.php 6709 2010-05-20 10:04:21Z jiaowei $
 */
class friends_model_star extends Model
{
	const PAGE_SIZE = 10;

	public function __construct()
	{
		parent::__construct();
		$this->table 		  = 'prop_standard';
		$this->category_table = 'app_star_category';
		$this->friends_table  = 'app_attention';
	}
	
	/**
	 * 获取用户详细信息
	 *
	 * @param int $uid
	 * @return array
	 */
	public function getUser($uid)
	{
		return $this->db->fetchRow("SELECT * FROM {$this->table} WHERE id=&id", array('id' => $uid));
	}
	
	/**
	 * 修改用户信息
	 *
	 * @param array $data
	 * @param int $uid
	 * @return bool
	 */
	public function updateUser($data, $uid)
	{
		return $this->db->update($this->table, $data, array('id' => $uid), 'id=&id');
	}
	
	/**
	 * 添加名人机构分类
	 *
	 * @param array $data
	 * @return bool
	 */
	public function addCate($data)
	{
		return $this->db->insert($this->category_table, $data);
	}
	
	/**
	 * 编辑分类
	 *
	 * @param array $data
	 * @param int $cid
	 * @return bool
	 */
	public function editCate($data, $cid)
	{
		return $this->db->update($this->category_table, $data, array('id' => $cid), 'id=&id');
	}
	
	/**
	 * 获取所有一级分类
	 *
	 * @return array
	 */
	public function getParentCate()
	{
		return $this->db->fetchAll("SELECT * FROM {$this->category_table} WHERE parent_id=0");
	}
	
	/**
	 * 获取一级分类下的子分类
	 *
	 * @param int $cateId
	 * @return array
	 */
	public function getChildCate($cateId)
	{
		return $this->db->fetchAll("SELECT * FROM {$this->category_table} WHERE parent_id = &cateId", array('cateId' => $cateId));
	}
	
	/**
	 * 获取所有名人机构分类(用户后台分类列表)
	 *
	 * @return array
	 */
	public function getAllCate()
	{
		return $this->db->fetchAll("SELECT * FROM {$this->category_table}");
	}
	
	/**
	 * 获取单个用户状态
	 *
	 * @param int $uid
	 * @return array
	 */
	public function getUserInfo($uid)
	{
		return $this->db->fetchRow("SELECT username,type FROM {$this->table} WHERE id=&id", array('id' => $uid));
	}
	
	/**
	 * 获取名人机构列表
	 *
	 * @param int $cate1	一级分类
	 * @param int $cate2	二级分类
	 * @return array
	 */
	public function getStars($cate1 = 0, $cate2 = 0, $fields = false)
	{
		$where  = '';
		$params = array();
		if ($cate1 != 0) {
			$where .= " AND cate_1=&cate1";
			$params['cate1'] = $cate1;
		}
		if ($cate2 != 0) {
			$where .= " AND cate_2=&cate2";
			$params['cate2'] = $cate2;
		}
		$sql = "SELECT " . ($fields ? '*' : 'id') . " FROM {$this->table} WHERE type > 0 {$where}";
		return $this->db->fetchAll($sql, $params);
	}
	
	/**
	 * 我加的名人机构
	 *
	 * @param int $uid
	 * @return array
	 */
	public function myStars($uid)
	{
		return $this->db->fetchAll("SELECT `host_id` FROM {$this->friends_table} WHERE is_friend=2 AND uid=&uid", array('uid' => $uid));	
	}
	
	/**
	 * 好友添加的名人机构
	 *
	 * @param array $friendIds
	 * @return array
	 */
	public function friendsStars($friendIds)
	{
		$friendIds = (array) $friendIds;
		$friendIds = implode(',', $friendIds);
		return $this->db->fetchAll("SELECT `host_id` FROM {$this->friends_table} WHERE is_friends=2 AND uid IN({$friendIds})");
	}



}
